var ws, aid, spage, total, uid, bid;
document.addEventListener('plusready',function(){
	uid = plus.storage.getItem('uid');
	ws = plus.webview.currentWebview();
	aid = ws.aid;//病例（订单）Id
	spage = ws.spage;
	total = ws.total;//处方费
	bid = ws.bid;//病人Id
//	console.log(spage);
	var dat = plus.storage.getItem('slist');
	var data = $.parseJSON(dat);
	$.each(data, function(k,v) {
		if(v.id == aid){
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(v)); 
			var arr ='';
			switch(v.status){
				case '1':arr = '待接单';break;
				case '2':arr = '待确认';$('.rz-btn').hide();break;
				case '3':arr = '已完成';$('.rz-btn').hide();break;
			}
			$('.line-h-44').text(arr);
			$('.name').text(v.binfo.name);
			if(v.binfo.sex == 1){
				$('.sex').text('女');
			} 
			if(v.binfo.age != 0 && v.binfo.age)$('.age').text(v.binfo.age);
			if(v.binfo.marry == 0){
				$('.marry').text('未婚');
			}
			$('.nation').text(v.binfo.nation);
			$('.phone').text(v.binfo.phone);
			$('.address').text(v.binfo.address);
			$('.dizhi').text(v.mailaddress);
			if(v.newly == 1){
				$('.newly').text('复诊');
			}
			$('.ztime').text(v.ztime);
			$('.history').text(v.history);
			//处方
			if(v.recipe){
				var recipe = v.recipe.split(';');
				var str = '';
				$.each(recipe, function(key,value) {
					if(value == ''){
						return true;
					}
					var rec = value.split(',');
					str += '<li>'+
								'<span>'+rec[0]+'</span><span class="mui-pull-right mui-h5">'+rec[1]+'</span>'+
							'</li>';
				});
				$('.recipe').prepend(str);
			}
			$('.zongji').text(v.orderprice+"元");
			if(v.paytype == 1){
				$('.jisong').text('快递寄送');
			}else{
				$('.jisong').text('来店自取');
				$('.mail').hide();
			}
		}
	})
	//确认订单
	$('.rz-btn').click(function(){
		$.ajax({
			type:"get",
			url:apiRoot+"?m=home&c=medicinal&a=updateStatus",
			data :{
				aid :aid,
				status :2
			},
			success :function(data){
				console.log(JSON.stringify(data));
				if(data >0){
					source();
				}
			},
			error :function(e){
				console.log(JSON.stringify(e));
				errortoast();
			}
		});
	})
},false)
//添加收入支出记录
function source(){
	var p = aid+'号订单记录';
	var money = parseFloat(total) * 0.2;
	var price = money.toFixed(2);
	console.log(p+" "+price);
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=income&a=recharge",
		data :{
			payer :bid,//支付的人
			earner :uid,//得到钱的人
			source :p,//什么渠道
			money :price,//金额
			tid :77//收入
		},
		dataType :'json',
		success :function(data){
			console.log(JSON.stringify(data));
			if(data > 0){
				if(spage){
					plus.webview.getWebviewById(spage).reload();
				}
				toast('提交成功');
				setTimeout(function(){
					ws.close();
				},2000);
			}
		},
		error :function(e){
			errortoast();
		}
	});
}